const httpUtil = require('_utils/httpUtil');
import { envConfig } from '../config/index';

class CrudService {
  constructor(resName) {
    this.baseUrl = `${envConfig.XY_API_URL}/${resName}`;
  }

  queryAll() {
    return httpUtil.request({
      url: `${this.baseUrl}`,
      method: 'GET',
      header: {
        Authorization: `Bearer ${getApp().globalData.token}`,
      },
    });
  }

  queryById(id) {
    return httpUtil.request({
      url: `${this.baseUrl}/${id}`,
      method: 'GET',
      header: {
        Authorization: `Bearer ${getApp().globalData.token}`,
      },
    });
  }

  create(item) {
    return httpUtil.request({
      url: `${this.baseUrl}`,
      method: 'POST',
      header: {
        Authorization: `Bearer ${getApp().globalData.token}`,
      },
      data: item,
    });
  }

  update(item) {
    return httpUtil.request({
      url: `${this.baseUrl}/${item.id}`,
      method: 'POST',
      header: {
        Authorization: `Bearer ${getApp().globalData.token}`,
      },
      data: item,
    });
  }

  delete(id) {
    return httpUtil.request({
      url: `${this.baseUrl}/${id}`,
      method: 'DELETE',
      header: {
        Authorization: `Bearer ${getApp().globalData.token}`,
      },
    });
  }
}

module.exports = CrudService;
